package hsrt.com.dynamiccomponent_android;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;

import java.util.ArrayList;
import java.util.Map;

import hsrt.com.dynamiccomponent_android.dynamic_component.DynamicComponentAdapter;
import hsrt.com.dynamiccomponent_android.dynamic_component.DynamicComponentInterface;
import hsrt.com.dynamiccomponent_android.dynamic_component.DynamicDisplayManager;

public class MainActivity extends AppCompatActivity implements DynamicDisplayManager.DynamicDisplayManagerListener {
    private DynamicDisplayManager dynamicDisplayManager;
    private DynamicComponentAdapter dynamicComponentAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        DynamicParser dynamicParser = new DynamicParser();

        dynamicDisplayManager = new DynamicDisplayManager(this, dynamicParser, false);

        ArrayList<DynamicComponentInterface> dynamicComponentInterfaceArrayList = new ArrayList<>();
        dynamicComponentInterfaceArrayList.add(new Component1());
        dynamicComponentInterfaceArrayList.add(new Component2());
        dynamicDisplayManager.registerItem(dynamicComponentInterfaceArrayList);

        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        dynamicComponentAdapter = new DynamicComponentAdapter(null, dynamicDisplayManager);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(dynamicComponentAdapter);


        recyclerView.postDelayed(new Runnable() {
            @Override
            public void run() {
                dynamicDisplayManager.putData(null, null);
            }
        }, 1000);
    }


    @Override
    public BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> getAdapter() {
        return dynamicComponentAdapter;
    }

    @Override
    public void validationFail(Map<String, Object> preUploadData, String failMsg) {
        Toast.makeText(this, failMsg, Toast.LENGTH_SHORT).show();
    }
}
